package com.digitalchina.lxnsy.activity.management;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.LandVO;
import com.digitalchina.lxnsy.toolkit.CustomDialog;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.AdminXML;
import com.lzy.okhttputils.model.HttpParams;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity.management
 * 创建人：郑皓
 * 创建时间：2019/4/23 20:21
 * 修改备注：
 * ================================================
 */
public class AddLandActivity extends BaseActivity {

    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.bt_save)
    Button btSave;

    private LandVO vo;
    private String LandParcelID = "";


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_add_land);
        setTitle("添加地块");
    }

    @Override
    public void initMember() {
        vo = (LandVO) getIntent().getSerializableExtra("LandVO");
        if (vo != null && vo.getIndex() > -1){
            LandParcelID = vo.getLandParcelId();
            etName.setText(vo.getLandParcelName());
        }

    }

    @Override
    public void addListener() {
        btSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!StringUtil.isEditEmpty(etName)) {
                    addLand();
                } else
                    showToast("请填写地块名称");

            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0: // 保存
                        String id = FastJsonUtil.getString(message.obj.toString(),"landParcelInfoId");
                        Intent i = new Intent();
                        vo.setLandParcelName(etName.getText().toString());
                        vo.setLandParcelId(id);
                        i.putExtra("LandVO", vo);
                        setResult(RESULT_OK, i);
                        CustomDialog.nativeDialogFinish(activity, "保存成功");
                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    private void addLand() {

        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        params.put("resNo", AdminXML.getResNo(activity));
        params.put("LandParcelID", LandParcelID);
        params.put("name", etName.getText().toString());

        OkHttpUtil.post(activity, URL.URL_SAVE_LAND, "正在保存", params, mHandler,
                MSG_S_0, MSG_F_0);
    }
}
